package com.lzw.controller;

import com.alibaba.fastjson.JSON;
import com.lzw.bean.bill;
import com.lzw.dto.BillQuery;
import com.lzw.service.SmbmsBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private SmbmsBillService billService;

    //TODO 条件查询、分页
    @GetMapping("/query")
    public ModelAndView pageListBillQuery(ModelAndView modelAndView, BillQuery billQuery) {
        List<bill> billList = billService.getBillList(billQuery);
        modelAndView.addObject("billList",billList);
        modelAndView.setViewName("/jsp/billlist");

        return modelAndView;
    }

    @DeleteMapping("/{billid}")
    public void delBillById(HttpServletResponse response, @PathVariable String billid) throws IOException {
        HashMap<String, String> resultMap = new HashMap<String, String>();
        boolean flag = billService.delBillById(billid);
        if(flag){//删除成功
            resultMap.put("delResult", "true");
        }else{//删除失败
            resultMap.put("delResult", "false");
        }
        //把resultMap转换成json对象输出
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter outPrintWriter = response.getWriter();
        outPrintWriter.write(JSON.toJSONString(resultMap));
        outPrintWriter.flush();
        outPrintWriter.close();

    }

    @GetMapping("/modify")
    public ModelAndView modifyBillPage(ModelAndView modelAndView,String billid) {
        bill bill = billService.getBillById(billid);
        modelAndView.addObject("bill",bill);
        modelAndView.setViewName("/jsp/billmodify");
        return modelAndView;
    }

    @GetMapping("/view")
    public ModelAndView viewBillPage(ModelAndView modelAndView,String billid) {
        bill bill = billService.getBillById(billid);
        modelAndView.addObject("bill",bill);
        modelAndView.setViewName("/jsp/billview");
        return modelAndView;
    }


    @GetMapping("/add")
    public ModelAndView addBillPage(ModelAndView modelAndView){
        modelAndView.setViewName("/jsp/billadd");
        return modelAndView;
    }



}
